package utils;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class DBUtils {
    private static String driver;
    private static String url;
    private static String user;
    private static String password;
    private static Connection connection;

    static {

        Properties properties = new Properties();
        InputStream inputStream = DBUtils.class.getResourceAsStream("/jdbc.properties");
        try {
            properties.load(inputStream);
            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            user = properties.getProperty("user");
            password = properties.getProperty("password");
            connection = DriverManager.getConnection(url, user, password);
            System.out.println("链接成功");
            Class.forName(driver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static ResultSet query(String sql, Object...param) throws SQLException {
        PreparedStatement statement=connection.prepareStatement(sql);
        for(int i=0;i<param.length;i++){
            statement.setObject(i+1,param[i]);
        }
        return statement.executeQuery();
    }

    public static int update(String sql,Object...param) throws SQLException {
        PreparedStatement statement=connection.prepareStatement(sql);
        for(int i=0;i<param.length;i++){
            statement.setObject(i+1,param[i]);
        }
        return statement.executeUpdate();
    }
}

